$(document).ready(function() {

	inicializarMascaras();
//	carregarMensagens();
	
});

function testeFB(){
	FB.ui(
			  {
			   method: 'feed',
			   name: 'The Facebook SDK for Javascript',
			   caption: 'Bringing Facebook to the desktop and mobile web',
			   description: (
			      'A small JavaScript library that allows you to harness ' +
			      'the power of Facebook, bringing the user\'s identity, ' +
			      'social graph and distribution power to your site.'
			   ),
			   link: 'https://developers.facebook.com/docs/reference/javascript/',
			   picture: 'http://www.fbrell.com/public/f8.jpg'
			  },
			  function(response) {
			    if (response && response.post_id) {
			      alert('Post was published.');
			    } else {
			      alert('Post was not published.');
			    }
			  }
			);
	
}


function login(){
	FB.login(function(response) {
		   if (response.authResponse) {
		     var access_token =   FB.getAuthResponse()['accessToken'];
		     var url = "/search?q='Igor Furtado'&type=user&fields=id,name,picture&access_token=" + access_token;
		     FB.api(url, function(response) {
		    	 for (var i = 0; i < response.data.length; i++) {
		    		 $('#resultGraph').val($('#resultGraph').val() + response.data[i].id + "<>" + response.data[i].name + "<>" + response.data[i].picture.data.url + "<>");
		    	 }
		     });
		   } else {
		     console.log('User cancelled login or did not fully authorize.');
		   }
		 }, {scope: ''});
}

function buscarUsuariosFacebook(){
	 $.ajaxSetup({ cache: true });
	  $.getScript('//connect.facebook.net/en_UK/all.js', function(){
	    FB.init({
	      appId: '476982669054953',
	      channelUrl: 'http://localhost:8080/sunrise/channel.html',
	    });     
	    $('#loginbutton,#feedbutton').removeAttr('disabled');
	    FB.getLoginStatus(updateStatusCallback);
	  });
	
	
	FB.login(function(response) {
		   if (response.authResponse) {
			 var usuario = $("#txtBuscarUsuario").val();  
			 var usuario_esc = escape(usuario);
		     var access_token =   FB.getAuthResponse()['accessToken'];
		     var url = "/search?q='"+ usuario_esc + "'&type=user&fields=id,name,picture&access_token=" + access_token;
		     FB.api(url, function(response) {
		    	 for (var i = 0; i < response.data.length; i++) {
		    		 $('#resultGraph').val($('#resultGraph').val() + response.data[i].id + "<>" + response.data[i].name + "<>" + response.data[i].picture.data.url + "<>");
		    	 }
		    	 $("#btnCarregarUsuarios").click();
		     });
		   } else {
		     console.log('User cancelled login or did not fully authorize.');
		   }
		 }, {scope: ''});
}


function inicializarMascaras() {
	$('.numerico1').setMask("numerico1");
	$('.numerico2').setMask("numerico2");
	$('.numerico3').setMask("numerico3");
	$('.numerico4').setMask("numerico4");
	$('.numerico5').setMask("numerico5");
	$('.numerico6').setMask("numerico6");
	$('.numerico7').setMask("numerico7");
	$('.numerico8').setMask("numerico8");
	$('.numerico9').setMask("numerico9");
	$('.numerico15').setMask("numerico15");
	$('.percentual').setMask("percentual");
	$('.moeda').setMask("moeda");
	$('.cnpj').setMask("cnpj");
	$('.cpf').setMask("cpf");
	$('.cep').setMask("cep");
	$('.telefone').setMask("telefone");
	$('.data').setMask("date");
}

function recarregarJS() {
	inicializarMascaras();
	carregarMensagens();
}

function formatarCNPJ() {
	$('.cnpj').setMask("cnpj");
}

function formatarData() {
	$('.data').setMask("date");
}

function formatarCPF() {
	$('.cpf').setMask("cpf");
}

function formatarCEP() {
	$('.cep').setMask("cep");
}

function formatarData() {
	$('.date input').setMask("date");
}

function formatarTelefone() {
	$('.telefone').setMask("telefone");
}

function formatarPercentual() {
	$('.percentual').setMask("percentual");
}

function formatarMoeda() {
	$('.moeda').setMask("moeda");
}

function formatarNumerico15() {
	$('.numerico15').setMask("numerico15");
}

function carregarMensagens() {
	$('.ui-inputfield').focus(function keypress(element) {
		$("#hiddenMsgInput").val(element.target.id);
		$("#hiddenMsgButton").click();
	});

	$('.ui-inputfield').blur(function keypress(element) {
		$("#hiddenBlurMsgButton").click();
	});
}

function carregarCadastroMenu() {
	$('.cadastroMenu').click();
}

function carregarAlterarProduto() {
	$('.alterarDetalheProduto').click();
}

function alerta(arg) {
	alert(arg);
}

function validarMovimentacao() {
	var movimentacaoValida = $("#salvoHidden").val();
	if (movimentacaoValida == 'true') {
		confirmarSalvarMovimentacao.show();
	}
}

function validarVenda() {
	var movimentacaoValida = $("#salvoHidden").val();
	if (movimentacaoValida == 'true') {
		confirmarSalvarVenda.show();
	}
}

function validarAjuste() {
	var ajusteValido = $("#salvoHidden").val();
	if (ajusteValido == 'true') {
		confirmacaoAjusteEstoque.show();
	}
}

function ShowMyName() {
	FB.login(function(response) {
		   if (response.authResponse) {
		     var access_token =   FB.getAuthResponse()['accessToken'];
		     console.log('Access Token = '+ access_token);
		     FB.api('/me', function(response) {
		    	 for (var i = 0; i < response.data.length; i++) {
		    	     response.data[i];
		    	     //Do something
		    	 }
		    	 console.log('Good to see you, ' + response.name + '.');
		    	 
		    	 
		     });
		   }
		 }, {scope: ''});
}